package models.admin;

import models.BaseModel;
import models.constants.DeletedStatus;
import models.enums.TreeNodeStatus;

import javax.persistence.*;
import java.util.List;

/**
 * Created by liming on 16/6/29.
 */
@Entity
@Table(name = "admin_dept")
public class AdminDept extends BaseModel {

    /**
     * 部门编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 部门名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 父节点ID
     */
    @Column(name = "parent_id")
    public Long parentId;

    /**
     * 是否为叶子节点
     */
    @Column(name = "is_leaf")
    public Boolean isLeaf;

    /**
     * 是否为叶子节点 open:叶子节点 close:非叶子节点
     */
    @Transient
    @Enumerated(EnumType.STRING)
    public TreeNodeStatus state;

    /**
     * 节点全路径
     */
    @Column(name = "node_id_path")
    public String nodeIdPath;

    /**
     * 排序号
     */
    @Column(name = "show_order")
    public Integer showOrder = 0;

    @Column(name = "remark")
    public String remark;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 创建人ID
     */
    @Column(name = "ct_user_id")
    public Long ctUserId;


    /**
     * 加载孩子节点
     *
     * @param id
     * @return
     */
    public static List<AdminDept> loadChildAdmnDept(long id) {
        List<AdminDept> adminDeptList = AdminDept.find("parentId = ? and deleted = ? order by showOrder,id desc", id, DeletedStatus.UN_DELETED).fetch();
        for (AdminDept adminDept : adminDeptList) {
            adminDept.state = adminDept.isLeaf != null && adminDept.isLeaf ? TreeNodeStatus.open : TreeNodeStatus.closed;
        }
        return adminDeptList;
    }

    /**
     * 删除部门
     *
     * @param deleteAdminDeptList
     */
    public static void deleteAdminDepts(Long[] deleteAdminDeptList) {
        for (Long adminDeptId : deleteAdminDeptList) {
            AdminDept adminDept = AdminDept.findById(adminDeptId);
            adminDept.deleted = DeletedStatus.DELETED;
            adminDept.save();
            Long brotherQty = AdminDept.loadBrotherQty(adminDept);
            if (brotherQty == 0) {
                AdminDept parentAdminDept = AdminDept.findById(adminDept.parentId);
                parentAdminDept.isLeaf = true;
                parentAdminDept.save();
            }
        }

    }

    /**
     * 判断部门是否被使用
     */
    public static Boolean checkIsUsed(long id) {
        return AdminUser.count("adminDept.id=?", id) > 0;
    }

    /**
     * 获取部门兄弟节点数量
     *
     * @param adminDept
     * @return
     */
    public static long loadBrotherQty(AdminDept adminDept) {
        return AdminDept.count("parentId = ? and deleted = ?", adminDept.parentId, DeletedStatus.UN_DELETED);
    }

    /**
     * 加载所有部门
     *
     * @return
     */
    public static List<AdminDept> loadAllDept() {
        List<AdminDept> deptList = AdminDept.find("deleted = ?", DeletedStatus.UN_DELETED).fetch();
        return deptList;
    }

}
